package com.vincent.suchao.lcld;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;

public class TabHostActivity extends TabActivity implements OnCheckedChangeListener {
	private String TAB1 = "TabOne";
	private String TAB2 = "TabTwo";
	private String TAB3 = "TabThree";
	private String TAB4 = "TabFour";
	public final static String TABACTIVITY_TAG = "MainActivity";
	private TabHost myTabhost;
	private Intent mTabOne;
	private Intent mTabTwo;
	private Intent mTabThree;
	private Intent mTabFour;
	private RadioButton rb_tabHistory;
	private RadioButton rb_tabDialer;
	private RadioButton rb_tabContacts;
	private RadioButton rb_tab4;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tabhost);
		this.mTabOne = new Intent(this, MainActivity.class);
		this.mTabTwo = new Intent(this, TDJS_Activity.class);
		this.mTabThree = new Intent(this, HZLC_Activity.class);
		this.mTabFour = new Intent(this, CGAL_Activity.class);
		mTabTwo.putExtra(TABACTIVITY_TAG, 1);
		mTabThree.putExtra(TABACTIVITY_TAG, 1);
		mTabFour.putExtra(TABACTIVITY_TAG, 1);
		init();
		setupIntent();
	}

	private void init() {
		rb_tabHistory = ((RadioButton) findViewById(R.id.rb_tabHistory));
		rb_tabHistory.setOnCheckedChangeListener(this);
		rb_tabDialer = ((RadioButton) findViewById(R.id.rb_tabDialer));
		rb_tabDialer.setOnCheckedChangeListener(this);
		rb_tabContacts = ((RadioButton) findViewById(R.id.rb_tabContacts));
		rb_tabContacts.setOnCheckedChangeListener(this);
		rb_tab4 = ((RadioButton) findViewById(R.id.rb_tab4));
		rb_tab4.setOnCheckedChangeListener(this);
	}

	private void setupIntent() {
		this.myTabhost = getTabHost();
		TabHost localTabHost = this.myTabhost;
		localTabHost.addTab(buildTabSpec(TAB2, R.string.tab_two, R.drawable.ic_launcher, this.mTabTwo));
		localTabHost.addTab(buildTabSpec(TAB1, R.string.tab_one, R.drawable.ic_launcher, this.mTabOne));
		localTabHost.addTab(buildTabSpec(TAB3, R.string.tab_three, R.drawable.ic_launcher, this.mTabThree));
		localTabHost.addTab(buildTabSpec(TAB4, R.string.tab_four, R.drawable.ic_launcher, this.mTabFour));
		rb_tabHistory.setClickable(false);
		rb_tabHistory.setChecked(true);
		rb_tabDialer.setClickable(true);
		rb_tabContacts.setClickable(true);

	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) {
		return this.myTabhost.newTabSpec(tag).setIndicator(getString(resLabel), getResources().getDrawable(resIcon)).setContent(content);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {

			switch (buttonView.getId()) {
			case R.id.rb_tabHistory:

				rb_tabHistory.setClickable(false);
				rb_tabDialer.setClickable(true);
				rb_tabContacts.setClickable(true);
				rb_tab4.setClickable(true);
				this.myTabhost.setCurrentTabByTag(TAB1);
				break;
			case R.id.rb_tabDialer:

				rb_tabHistory.setClickable(true);
				rb_tabDialer.setClickable(false);
				rb_tabContacts.setClickable(true);
				rb_tab4.setClickable(true);
				this.myTabhost.setCurrentTabByTag(TAB2);
				break;
			case R.id.rb_tabContacts:

				rb_tabHistory.setClickable(true);
				rb_tabDialer.setClickable(true);
				rb_tabContacts.setClickable(false);
				rb_tab4.setClickable(true);
				this.myTabhost.setCurrentTabByTag(TAB3);
				break;

			case R.id.rb_tab4:

				rb_tabHistory.setClickable(true);
				rb_tabDialer.setClickable(true);
				rb_tabContacts.setClickable(true);
				rb_tab4.setClickable(false);
				this.myTabhost.setCurrentTabByTag(TAB4);
				break;
			default:
				break;
			}
		}
	}
}
